app.controller('goodsDetailInfoCtrl',['$scope','$stateParams','utilService','chainGoodsDetailInfoService','toaster',function ($scope,$stateParams,utilService,chainGoodsDetailInfoService,toaster) {

    $scope.id=$stateParams.id;
    $scope.goods;
    $scope.findByIdForOther=function(){
        chainGoodsDetailInfoService.findByIdForOther($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.goods=result.data;
                    $scope.skuPrice=$scope.goods.price;
                    $scope.skuStock=$scope.goods.stock;
                    break;
                case 646:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    $scope.findByIdForOther();


    //查询商品图片
    $scope.coverImages=[];
    $scope.cardImages=[];
    $scope.detailImages=[];
    $scope.commendImages=[];

    $scope.findByGoodsId=function(){
        chainGoodsDetailInfoService.findByGoodsId($scope).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data==null){
                        return;
                    }

                    if(!angular.isUndefined(result.data['0'])){
                        $scope.coverImages=result.data['0'];
                    }

                    if(!angular.isUndefined(result.data['1'])){
                        $scope.cardImages=result.data['1'];
                    }

                    if(!angular.isUndefined(result.data['2'])){
                        $scope.detailImages=result.data['2'];
                    }

                    if(!angular.isUndefined(result.data['3'])){
                        $scope.commendImages=result.data['3'];
                    }

                    $scope.skuImg=$scope.coverImages[0].fileName;

                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    $scope.findByGoodsId();

    //查询skuContent
    $scope.querySkuContent = function(){
        chainGoodsDetailInfoService.querySkuContent($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.skuContents=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    $scope.querySkuContent();

    //查询sku_item
    $scope.queryGoodsSku=function(){
        chainGoodsDetailInfoService.queryGoodsSku($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.skuItems=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    $scope.queryGoodsSku();


    //选中规格
    $scope.choose=function(subitem,item){
        for(var i=0;i<item.childrens.length;i++){
            if(item.childrens[i]===subitem){
                subitem.chooseState=!subitem.chooseState;
            }else{
                item.childrens[i].chooseState=false;
            }
        }

        var key='';
        $scope.standard='';
        for(var i=0;i<$scope.skuItems.length;i++){
            for(var j=0;j<$scope.skuItems[i].childrens.length;j++){
                if($scope.skuItems[i].childrens[j].chooseState){
                    key+=$scope.skuItems[i].childrens[j].skuItemsId+'_';
                    $scope.standard+=$scope.skuItems[i].childrens[j].standard+',';
                }
            }
        }
        key=key.substring(0,key.length-1);
        $scope.standard=$scope.standard.substring(0,key.length-1);

        $scope.chooseSuccess=true;
        for(var i=0;i<$scope.skuContents.length;i++){
            if($scope.skuContents[i].key==key){
                $scope.skuPrice=$scope.skuContents[i].price;
                $scope.skuStock=$scope.skuContents[i].stock;
                $scope.chooseSuccess=false;
            }
        }

        if($scope.chooseSuccess){
            $scope.skuPrice=$scope.goods.price;
            $scope.skuStock=$scope.goods.stock;
        }

    }


}])